﻿using Youz.Common.BLL;
using Youz.Common.Entity;
using Youz.Common.Helper;
using Youz.CreditRepay.BLL;
using Youz.CreditRepay.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace Youz.CreditRepay.Manage.Sys.Handlers
{
    /// <summary>
    /// Member_AreaHandler 的摘要说明
    /// </summary>
    public class Member_AreaHandler : IHttpHandler
    {

        Member_AreaBLL bll = new Member_AreaBLL();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string type = WebHelper.Get("type");
            string result = string.Empty;
            switch (type)
            {
                case "get":
                    result = GetOne();
                    break;
                case "list":
                    result = List();
                    break;
                case "save":
                    result = Save();
                    break;
                case "delete":
                    result = Delete();
                    break;
            }

            context.Response.Write(result);
            context.Response.End();
        }

        private string GetOne()
        {
            return bll.GetJson(WebHelper.Post<Int64>("AreaID"));
        }

        private string List()
        {
            int pageIndex = WebHelper.Post<int>("start");
            int pageSize = WebHelper.Post<int>("length");
            string sqlwhere = string.Empty;
            string AreaName = WebHelper.Post("AreaName");
            if (!string.IsNullOrWhiteSpace(AreaName))
            {
                sqlwhere += string.Format("AreaName='{0}'", AreaName);
            }
            int total;
            DataTable dt = bll.Gets(sqlwhere, pageIndex, pageSize, out total);
            return Utility.GetJsonByDataTableHaveTime(total, dt);
        }


        private string Save()
        {
            string id = WebHelper.Post("AreaID");
            bool isAdd = string.IsNullOrWhiteSpace(id);
            Member_Area model = new Member_Area();

            if (isAdd)
            {
                model.Creator = Core.MemberAccount;
                model.CreateTime = DateTime.Now;
            }
            else
            {
                model = bll.Get(WebHelper.Post<Int64>("AreaID"));
            }

            model.AreaName = WebHelper.Post<String>("AreaName");
            model.AgentsAccount = WebHelper.Post<String>("AgentsAccount");
            model.Memo = WebHelper.Post<String>("Memo");

            if (string.IsNullOrWhiteSpace(model.AreaName))
                return Utility.AjaxResult(false, "区域名称不能为空");

            model.Modifier = Core.MemberAccount;
            model.ModifyTime = DateTime.Now;

            if (isAdd ? bll.Add(model) : bll.Update(model))
            {
                return Utility.AjaxResult(true, AjaxResultType.Save);
            }
            else
            {
                return Utility.AjaxResult(false, AjaxResultType.Save);
            }
        }
        private string Delete()
        {
            if (bll.Delete(WebHelper.Post<Int64>("AreaID")))
            {
                return Utility.AjaxResult(true, AjaxResultType.Delete);
            }
            else
            {
                return Utility.AjaxResult(false, AjaxResultType.Delete);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}